package com.group01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group01.pojo.EsLog;
import com.group01.result.PageTemplate;
import com.group01.result.Result;
import com.group01.service.EsLogService;
import com.group01.mapper.EsLogMapper;
import com.group01.utils.BeanCopyUtils;
import com.group01.vo.Log.LogGetPageVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 19559
* @description 针对表【es_log(日志表)】的数据库操作Service实现
* @createDate 2024-06-22 20:54:29
*/
@Service
public class EsLogServiceImpl extends ServiceImpl<EsLogMapper, EsLog> implements EsLogService{

    /**
     * 注入
     */
    private final EsLogMapper esLogMapper;

    public EsLogServiceImpl(EsLogMapper esLogMapper) {
        this.esLogMapper = esLogMapper;
    }

    /**
     * 分页+条件查询日志信息
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @param username 操作人用户名
     * @return {@link Result }<{@link List }<{@link EsLog }>>
     */
    @Override
    public Result<PageTemplate<LogGetPageVO>> getLogPage(Integer pageNum, Integer pageSize, String username) {
        LambdaQueryWrapper<EsLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(username) , EsLog::getUsername, username);

        Page<EsLog> page = new Page<>(pageNum, pageSize);
        esLogMapper.selectPage(page, queryWrapper);

        List<LogGetPageVO> vos = BeanCopyUtils.copyBeanList(page.getRecords(), LogGetPageVO.class);
        return Result.success("查询日志成功",new PageTemplate<>(page.getTotal(), vos));
    }

    /**
     * 删除+批量删除日志信息
     * @param ids 日志id集合
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> deleteLog(String ids) {
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        esLogMapper.deleteBatchIds(idList);
        return Result.success("批量删除日志成功", null);
    }
}




